package com.dgshanger.sanhxiaofeisc.ui.person

import android.content.Intent
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.PhoneUtils
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.entity.CustomServerEntity
import com.dgshanger.sanhxiaofeisc.ui.WebActivity
import com.xx.baseuilibrary.mvp.BaseMvpViewActivity
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.RxHttpObserver
import kotlinx.android.synthetic.main.activity_service.*


/**
 * ServiceActivity
 * 沉迷学习不能自拔
 * Describe：联系客服
 * Created by 雷小星🍀 on 2018/4/16 14:01.
 */
class ServiceActivity : BaseMvpViewActivity() {

    override fun getActivityLayoutId(): Int = R.layout.activity_service

    private var servicePhone = ""
    private var serviceUrl = ""

    override fun initData() {
        AppApi.Api()
                .custom_server()
                .compose(RxHelper.io_main())
                .subscribe(object : RxHttpObserver<CustomServerEntity>() {
                    override fun onCompleted(msg: String?, entity: CustomServerEntity?) {
                        servicePhone = entity!!.custom_server_contract
                        serviceUrl = entity.custom_server_online_url
                    }

                    override fun onError(error: String?) {
                    }

                    override fun onLoadingStart() {
                    }

                    override fun onLoadingFinish() {
                    }

                })
    }

    override fun initEvent() {
        view_online.setOnClickListener {
            //在线客服
            val intent = Intent(mContext, WebActivity::class.java).apply {
                putExtra("title", getString(R.string.online_customer_service))
                putExtra("url", serviceUrl)
            }
            startActivity(intent)
        }
        view_tel.setOnClickListener {
            //电话客服
            PermissionUtils.permission(android.Manifest.permission.CALL_PHONE)
                    .callback(object : PermissionUtils.SimpleCallback {
                        override fun onGranted() {
                            //获取通话权限后进行拨号
                            PhoneUtils.call(servicePhone)
                        }

                        override fun onDenied() {
                            showToast(getString(R.string.premission_tips))
                        }

                    })
                    .rationale(object : PermissionUtils.OnRationaleListener {
                        override fun rationale(shouldRequest: PermissionUtils.OnRationaleListener.ShouldRequest?) {
                            shouldRequest?.again(true)
                        }

                    })
                    .request()
        }
    }
}